home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-09-15 | 5.7 KB | 184 lines | [TEXT/ALFA] |
- #===========================================================================
- # 'Word Completion', in the spirit of Paul van Mulbregt's BBXT.
- #
- # Composed by Mark Nagata (nagata@kurims.kyoto-u.ac.jp)
- # for Alpha 5.76, 4/22/94.
- #
- # Modified by Tim van der Leeuw (tnleeuw@cs.vu.nl), 9/14/94.
- # In the spirit of emacs.
- # I have modified this routine extensively to add the ability to complete
- # a word in multiple ways if called repeatedly.
- # For this purpose, I have introduced all the global variables (starting with
- # __wc__) and added the first if-statement -- everything between
- # 'set pos [getPos]' and 'backwardWord'. In the original routine, I've changed
- # some existing variables to globals, prefixing their name with '__wc__'.
- #
- # This code is probably not the most efficient tcl-code, nor the most elegant
- # tcl-code for this problem, but hey, it is the first function I've ever
- # written in tcl!
- # If anyone has some suggestions for improvement, or
- # knows of a better algorithm, I would like to know it.
- #================================================================================
- set __wc__insPos -1
- proc wordCompletion {} {
- global __wc__len __wc__prevPos __wc__insPos __wc__prevFound __wc__pat __wc__nextStart __wc__fwd
-
- set pos [getPos]
- if $pos==$__wc__insPos {
- # Cursor changed place?
- set skipStr $__wc__prevFound
- while 1 {
- if $__wc__fwd {
- set fndMsg "found below."
- } else {
- set fndMsg "found above."
- }
- if {![catch {search -f $__wc__fwd -r 1 -i 0 -m 1 -- $__wc__pat $__wc__nextStart} data]} {
- set d00 [lindex $data 0]
- set beg [expr $d00+$__wc__len]
- set end [lindex $data 1]
- set __wc__prevFound [getText $d00 $end]
- if [string compare $skipStr $__wc__prevFound] {
- # Have we got the same word twice?
- set txt [getText $beg $end]
- deleteText $__wc__prevPos $__wc__insPos
- goto $__wc__prevPos
- insertText $txt
- message $fndMsg
- # Set a number of globals for possible next go-around
- set __wc__insPos [getPos]
- if $__wc__fwd {
- # Search Forwards
- set __wc__nextStart $end
- # End of found word
- } else {
- # Search Backwards
- set __wc__nextStart [expr $d00 - $__wc__len]
- # Before start of found word
- if $__wc__nextStart<=0 {
- set __wc__fwd 1
- set __wc__nextStart $__wc__insPos
- }
- }
- return
- } else {
- # Move start of search after finding string again
- if $__wc__fwd {
- # Searching Forwards
- set __wc__nextStart $end
- # End of found word
- } else {
- # Still Searching Backwards
- set __wc__nextStart [expr $d00 - $__wc__len]
- # Before start of found word
- if $__wc__nextStart<=0 {
- set __wc__fwd 1
- set __wc__nextStart $__wc__insPos
- }
- }
- }
- # End if string compare
- } else {
- # Search string not found
- if $__wc__fwd {
- # We were already looking forward, so the word is not in the file
- message "Not found."
- set __wc__insPos -1
- goto $pos
- backwardWordSelect
- return
- } else {
- # start looking forward
- set __wc__fwd 1
- set __wc__nextStart $__wc__insPos
- }
- }
-
- }
- }
- backwardWord
- # Start new search for WordCompletion
- set start [getPos]
- set one [getText $start $pos]
- set __wc__len [expr $pos-$start]
- set __wc__pat [append one {[a-zA-Z0-9_]+}]
- set start [expr $start-1]
- if {![catch {search -f 0 -r 1 -i 0 -m 1 -- $__wc__pat $start} data]} {
- set d00 [lindex $data 0]
- set beg [expr $d00+$__wc__len]
- set end [lindex $data 1]
- set __wc__prevFound [getText $d00 $end ]
- set txt [getText $beg $end]
- goto $pos
- insertText $txt
- message "found above."
- # Set a number of globals for possible next go-around
- set __wc__insPos [getPos]
- set __wc__prevPos $pos
- set __wc__nextStart [expr $d00-$__wc__len]
- set __wc__fwd 0
- return
- }
- if {![catch {search -f 1 -r 1 -i 0 -m 1 -- $__wc__pat $pos} data]} {
- set __wc__prevFound [getText [lindex $data 0] [lindex $data 1] ]
- set beg [expr [lindex $data 0]+$__wc__len]
- set end [lindex $data 1]
- set txt [getText $beg $end]
- goto $pos
- insertText $txt
- message "found below."
- # Set a number of globals for possible next go-around
- set __wc__insPos [getPos]
- set __wc__prevPos $pos
- set __wc__nextStart $end
- set __wc__fwd 1
- return
- }
- goto $pos
- backwardWordSelect
- }
-
-
-
- # This is all due to the idea of Paul van Mulbregt. In his documentation
- # of his BBEdit BBExtension (info-mac/text/bbedit-fl-package-11.hqx),
- # he explains:
- #
- # -- From the documentation written by --
- # -- Paul van Mulbregt (paulvm@dragonsys.com) --
- #
- # Word Completion
- #
- # This extension saves typing, as well as making sure variable names are
- # correct. While typing the following C code, there is no need to type all
- # of the second occurrence of verySpecialInt. One could copy and paste, but
- # another way is to type a few letters, and select the Word Completion
- # Extension.
- #
- # int verySpecialInt = 10;
- # while(verySp
- #
- #
- # becomes
- #
- # int verySpecialInt = 10;
- # while(verySpecialInt
- #
- #
- #
- # Word Completion will look back in the code to find the first match and then
- # extend the current occurrence to match the previous occurrence. If a match
- # is not found looking backwards, then it looks forwards. If not found
- # forwards, the word is selected. This is a quick way to save on typing,
- # good for helping prevent RSI, but perhaps more importantly, to make sure
- # that variable names are spelt correctly.
- #
- # There is no user interface for Word Completion.
- #
- # The usefulness of this extension is greatly enhanced if the extension is
- # bound to a key!
- #
- # -- end of Paul van Mulbregt's explanation. --
-
-
-